package com.wisdomparking.control.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.wisdomparking.bean.Manager;
import com.wisdomparking.config.result.TempResult;
import com.wisdomparking.constant.Constants;
import com.wisdomparking.control.BaseController;
import com.wisdomparking.service.ManagerService;
import com.wisdomparking.tools.MD5;

/**
 * 个人中心
 */
@RestController
@RequestMapping("/web/settings")
public class SettingsControl extends BaseController{
	
	@Autowired ManagerService managerService;
	
	/**
     * 首页
     *
     * @return
     */
    @RequestMapping(value = "/index")
    public ModelAndView index() {
    	
        return new ModelAndView("settings/index");
    }
    
	/**
     * 左侧
     *
     * @return
     */
    @RequestMapping(value = "/left")
    public ModelAndView left() {
        return new ModelAndView("settings/left");
    }
    
    
    /**
     * 用户信息
     *
     * @return
     */
    @RequestMapping(value = "/getLoginInfo")
    public ModelAndView getLoginInfo() {
    	
    	Manager manager = (Manager)session.getAttribute(Constants.LOGININFO);
    	if(manager.getManagerGrade()==2){
    		Manager upManager = managerService.getManager(manager.getId());
        	request.setAttribute("upManager", upManager);
    	}
    	
    	
    	return new ModelAndView("settings/userInfo");
    }
    
    
    /**
     * 修改密码页
     *
     * @return
     */
    @RequestMapping(value = "/getPwd")
    public ModelAndView getPwd() {
    	
    	return new ModelAndView("settings/pwd");
    }
    
    /**
     * 修改密码
     *
     * @return
     */
    @RequestMapping(value = "/updatePwd")
    public TempResult updatePwd(String oldpwd, String password){
    	boolean flag = false;
    	Manager manager = (Manager)session.getAttribute(Constants.LOGININFO);
    	if(manager.getPassword().equals(MD5.getMD5Str(oldpwd))){
    		manager.setPassword(MD5.getMD5Str(password));
    		flag = managerService.updateManager(manager, true);
    	}
    	
		return new TempResult(flag, "");
    }
    
    
}
